package com.lu.netty.handler;

import com.alibaba.fastjson.JSONObject;
import com.lu.netty.client.DefaultFuture;
import com.lu.netty.util.Response;
import io.netty.channel.*;


/**
 * @author xiaolu
 * @version 1.0
 * @date 2021-02-02 23:17
 */
public class SimpleClientHandler extends ChannelInboundHandlerAdapter {

    // 接收服务器返回的数据
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {

        if ("ping".equals(msg.toString())) {
            ctx.channel().writeAndFlush("pong\r\n");
            return;
        }

        //ctx.channel().attr(AttributeKey.valueOf("sss")).set(msg);
        Response response = JSONObject.parseObject(msg.toString(), Response.class);
        DefaultFuture.received(response);
        //ctx.channel().close();
    }

    @Override
    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
        super.userEventTriggered(ctx, evt);
    }
}
